import time
import pytest
from playwright.sync_api import sync_playwright, expect



@pytest.fixture
def page():
    # 初始化框架并启动后台
    with sync_playwright() as p:
        # 打开浏览器p.firefox.launch
        # 不要无头模式headless=False
        browser = p.firefox.launch(headless=False)
        # 打开新的页面
        page = browser.new_page()
        yield page
        page.close()


def test_jiuxian_01(page):
       #访问目标网页
        page.goto('https://www.jiuxian.com/')
        time.sleep(5)
        #开始编写脚本
        #点击去登录
        page.click('xpath = /html/body/div[2]/div/div[1]/div/a[1]')
        time.sleep(10)
        #输入账号密码
        #name定位
        page.fill(selector='[name="uname"]',value='13381144084')
        # page.fill('xpath = /html/body/div/form/div/div/div/div/div[1]/div[2]/div[1]/input','13381144084')
        #id定位
        page.fill(selector='#password',value='grq200566++')
        # page.fill('xpath = /html/body/div/form/div/div/div/div/div[1]/div[2]/div[2]/input[1]','grq200566++')
        time.sleep(15)
        #点击登录按钮
        page.click('xpath = /html/body/div/form/div/div/div/div/div[1]/div[2]/div[4]/input')
        time.sleep(8)

def test_jiuxian_02(page):
    test_jiuxian_01(page)
    # 点击搜索输入框
    page.fill('xpath = /html/body/div[4]/div/div[2]/div[1]/form/input[1]', '五粮液')
    page.click('xpath = /html/body/div[4]/div/div[2]/div[1]/form/input[2]')
    time.sleep(5)
    # 点击搜索
    page.click('xpath=/html/body/div[4]/div/div[2]/div[1]/form/input[2]')
    # 点击商品详情
    page.click('xpath = /html/body/div[6]/div/div[2]/div[5]/ul/li[1]/div[2]/div[1]/a/img')
    # 跳转窗口
    page.goto('https://www.jiuxian.com/goods-88665.html')
    time.sleep(4)
    # 加入购物车
    page.click(selector='#addToCartForDetail')

